package com.cw.jeeyt.service.impl.facade;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cw.jeeyt.service.bo.facade.FacadeParamBo;
import com.cw.jeeyt.service.converter.facade.FacadeParamConverter;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.facade.FacadeParamDao;
import com.cw.jeeyt.dao.model.facade.FacadeParamDo;
import com.cw.jeeyt.dao.query.facade.FacadeParamQuery;
import com.cw.jeeyt.service.facade.FacadeParamService;
import com.cw.jeeyt.service.utils.DataDicUtils;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 接口参数接口业务实现类
 *
 * @author chenw
 * @date 2019-11-01 23:46:50
 */
@Service
public class FacadeParamServiceImpl implements FacadeParamService {
    @Autowired
    private FacadeParamDao facadeParamDao;

    /**
     * 新增接口参数
     *
     * @param request 条件
     * @return String
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addFacadeParam(FacadeParamBo request) {
        if (request == null) {
            return null;
        }
        request.setCreateBy(SessionUtil.getSessionUserId());
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setCreateDate(new Date());
        request.setUpdateDate(new Date());
        facadeParamDao.insertBySelective(FacadeParamConverter.bo2Do(request));
        return "";
    }

    /**
     * 更新接口参数
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifyFacadeParam(FacadeParamBo request) {
        if (request == null) {
            return false;
        }
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setUpdateDate(new Date());
        facadeParamDao.updateBySelective(FacadeParamConverter.bo2Do(request));
        return true;
    }

    /**
     * 删除接口参数
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFacadeParam(FacadeParamBo request) {
        if (request == null) {
            return false;
        }
        return facadeParamDao.deleteByKey(request.getId()) > 0;
    }

    /**
     * 分页查询接口参数
     *
     * @param request 条件
     * @return PageInfo<FacadeParamBo>
     */
    @Override
    public PageInfo<FacadeParamBo> selectPage(FacadeParamBo request, PageParam pageParam) {
        PageInfo<FacadeParamDo> pageInfo = facadeParamDao.selectPage(FacadeParamQuery.builder()
            .id(request.getId())
            .paramCode(request.getParamCode())
            .paramName(request.getParamName())
            .paramType(request.getParamType())
            .publicParam(request.getPublicParam())
            .paramComment(request.getParamComment())
            .childParams(request.getChildParams())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build(), pageParam);
        List<FacadeParamBo> facadeParams = FacadeParamConverter.doList2BoList(pageInfo.getList());
        if (CollectionUtil.isNotEmpty(facadeParams)) {
            Map<String, Object> dictMap = new HashMap<>();
            dictMap.put("publicParam", "std_zb_yn");
            JSONArray jsonArray = DataDicUtils.convertDict(JSON.parseArray(JSON.toJSONString(facadeParams)), dictMap);
            facadeParams = JSON.parseArray(jsonArray.toJSONString(), FacadeParamBo.class);
        }
        return new PageInfo<>(pageInfo, facadeParams);
    }

    /**
     * 查询接口参数详情信息
     *
     * @param request 条件
     * @return FacadeParamBo
     */
    @Override
    public FacadeParamBo queryFacadeParamDetail(FacadeParamBo request) {
        if (request == null) {
            return null;
        }
        return FacadeParamConverter.do2Bo(facadeParamDao.selectByKey(request.getId()));
    }

    /**
     * 根据条件查询接口参数
     *
     * @param request 条件
     * @return List<FacadeParamBo>
     */
    @Override
    public List<FacadeParamBo> queryFacadeParamList(FacadeParamBo request) {
        if (request == null) {
            return null;
        }
        return FacadeParamConverter.doList2BoList(facadeParamDao.selectByExample(FacadeParamQuery.builder()
            .id(request.getId())
            .paramCode(request.getParamCode())
            .paramName(request.getParamName())
            .paramType(request.getParamType())
            .publicParam(request.getPublicParam())
            .paramComment(request.getParamComment())
            .childParams(request.getChildParams())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build()));
    }

    /**
     * 根据key查询接口参数
     *
     * @param id
     * @return
     */
    @Override
    public FacadeParamBo selectByKey(Long id) {

        return FacadeParamConverter.do2Bo(facadeParamDao.selectByKey(id));
    }
}
